import { defineStore } from 'pinia';
import { ref } from 'vue';

export const useClassStore = defineStore(
  'class',
  () => {
    const classid = ref('');
    const pageNum = ref(1);
    const pageSize = ref(12);
    const total = ref(0);
    const categoryDetailList = ref([]);
    const searchList = ref([]);
    const setClassId = v => (classid.value = v);
    const setPageNum = v => (pageNum.value = v);
    const setPageSize = v => (pageSize.value = v);
    const setTotal = v => (total.value = v);

    const setCategoryDetailList = arr => (categoryDetailList.value = arr);
    const addCategoryDetailList = arr => categoryDetailList.value.push(...arr);
    const setSearchList = arr => (searchList.value = arr);
    const addSearchList = arr => searchList.value.push(...arr);

    /* 保留分类 ID，只清分页相关 */
    const resetPagination = () => {
      pageNum.value = 1;
      total.value = 0;
      categoryDetailList.value = [];
    };
    /* 搜索结果分页 */
    const resetSearchPagination = () => {
      pageNum.value = 1;
      total.value = 0;
      searchList.value = [];
    };
    
    /* 完全重置 */
    const resetStore = () => {
      classid.value = '';
      resetPagination();
    };

    return {
      classid,
      pageNum,
      pageSize,
      total,
      categoryDetailList,
      searchList,
      setClassId,
      setPageNum,
      setPageSize,
      setTotal,
      setCategoryDetailList,
      addCategoryDetailList,
      resetPagination,
      resetSearchPagination,
      resetStore,
      setSearchList,
      addSearchList,

    };
  },
  {
    persist: {
      key: 'class-store',
      paths: ['classid', 'pageNum', 'pageSize', 'total', 'categoryDetailList', 'searchList'],
    },
  }
);
